package edu.del.leetcode;


import java.util.HashMap;
import java.util.Map;

/**
 * @BelongsProject: Algorithm-Repo
 * @BelongsPackage: edu.del.main.java.leetcode
 * @Author: Del
 * @CreateTime: 2025-07-13  23:52
 * @Description: 力扣第1题 - 两数之和
 * @solution 哈希表解法
 * @timeComplexity O(n)
 * @spaceComplexity O(n)
 * @Version: 1.0
 */
public class L0001_TwoSum {
    public int[] twoSum(int[] nums, int target) {
        Map<Integer, Integer> map = new HashMap<Integer, Integer>();
        for (int i = 0; i < nums.length; i++) {
            int complement = target - nums[i];
            if (map.containsKey(complement)) {
                return new int[]{map.get(complement), i};
            }
            map.put(nums[i], i);
        }
        throw new IllegalArgumentException("No solution");
    }
}
